{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "03c4502e",
   "metadata": {},
   "source": [
    "* 第五周笔记\n",
    "* 记录人：陈铮铮\n",
    "* 2022.9.20"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e9d4249",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "> 1. 有序的数据结构之列表\n",
    "\n",
    "> 2. 用户输入\n",
    "\n",
    "> 3. 关于格式化文本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98258c77",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_01_name = \"marry\"\n",
    "# 思考：如果想一个变量存放一个班同学的名字，怎么存？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4d3b4f0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Marry,Jine,Jack\n"
     ]
    }
   ],
   "source": [
    "网三_name_list = \"Marry,Jine,Jack\"\n",
    "print(网三_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5811b4bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jack'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考2：处理数据会不会麻烦\n",
    "网三_name_list[11:15]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bebe5109",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考3：想知道这个名单有多少字\n",
    "len(网三_name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a87661e",
   "metadata": {},
   "source": [
    "## 1.引入列表概念\n",
    "\n",
    "* 注释：我们已经学习了 数值 和 字符，list可以放数值和字符，但是通常要嘛一起放数值，要嘛一起放字符串。\n",
    "\n",
    "总结一些通用的有序数据结构的方法：\n",
    "\n",
    "> 1. 取值：通过索引值取出对应的内容\n",
    "> 2. 切片：通过索引值区间取出对应的一些内容\n",
    "> 3. in 可以查看成员对象是否存在\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "71435a6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "de7c8327",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Marry', 'Jine', 'Jack'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "网三_name_list = [\"Marry\",\"Jine\",\"Jack\"]\n",
    "print(网三_name_list,type(网三_name_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3f26c020",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网三: Marry\n",
      "网三: Jine\n",
      "网三: Jack\n"
     ]
    }
   ],
   "source": [
    "for i in 网三_name_list:\n",
    "    print(\"网三:\",i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9b573982",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list = [\"Marry\",\"Jine\",\"Jack\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6f5921c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Jine', 'Jack', '梁宝琳']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "375ae0da",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.append(\"梁宝琳\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7bfd4b34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Jine', 'Jack', '梁宝琳', '梁宝琳']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1c514e0d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Jine', 'Jack', '梁宝琳', '梁宝琳']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "da8953a2",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[1;32mIn [21]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43m网三_name_list\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremove\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mjine\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "网三_name_list.remove(\"jine\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e82aba7a",
   "metadata": {},
   "source": [
    "## 2.2 列表的方法（Function）--对列表数据进行操作\n",
    "\n",
    "> 1. 增\n",
    "> 2. 删\n",
    "> 3. 改\n",
    "> 4. 查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03821d78",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：移除19\n",
    "age_list = [18,19,18,19,18,19,20,21,17,19,18]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "1fed5282",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list.count(19)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d5240b31",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list = [18,19,18,19,18,19,20,21,17,19,18]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "ceeff9b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 19, 18, 19, 20, 21, 17, 19, 18]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法一\n",
    "删除的数量 = age_list.count(19)\n",
    "for i in range(删除的数量):\n",
    "    if 19 in age_list:\n",
    "        age_list\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "6c56b7ab",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 19\n",
      "2 19\n",
      "3 19\n",
      "6 19\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[18, 18, 18, 20, 21, 17, 18]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法二\n",
    "for i,v in enumerate(age_list):\n",
    "    if v == 19:\n",
    "        print(i,v)\n",
    "        age_list.pop(i)\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "47b01520",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 19, 18, 19, 20, 21, 17, 19, 18]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "88b6f477",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'builtin_function_or_method' object is not subscriptable",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [25]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mage_list\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpop\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m19\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'builtin_function_or_method' object is not subscriptable"
     ]
    }
   ],
   "source": [
    "age_list.pop[\"19\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fad8f573",
   "metadata": {},
   "source": [
    "# 课后实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "98085241",
   "metadata": {},
   "outputs": [],
   "source": [
    "python_class_count = [\"小陈\",\"小梁\",\"小黄\",\"小刘\",\"小张\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ec627330",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小陈', '小梁', '小黄', '小刘', '小张']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_class_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e22098bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "python_class_count.extend(\"小王\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "44d542eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小陈', '小梁', '小黄', '小刘', '小张', '小', '王']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_class_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8036143c",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "list.copy() takes no arguments (1 given)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [12]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mpython_class_count\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcopy\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m小陈\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mTypeError\u001b[0m: list.copy() takes no arguments (1 given)"
     ]
    }
   ],
   "source": [
    "python_class_count.copy(\"小陈\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e32bc5c",
   "metadata": {},
   "source": [
    "* 切片 a[:]\n",
    "* 访问元素 a[]\n",
    "* 增加元素1.append()→在尾部追加一个数 2.insert（）→可以在某个元素里面插入一个元素\n",
    "* 删除元素\n",
    "> 1. remove() 删除里面其中一个，如果有重复，就删最前面的\n",
    "> 2. pop()删除-1，就是最后一个值\n",
    "> 3.  clear() 全部清除\n",
    "* 更改元素 a[] ="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f4b2a0d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [\"小陈\",\"小梁\",\"小黄\",\"小刘\",\"小张\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "915ebc2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小陈', '小梁', '小黄', '小刘', '小张']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9102fb03",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小陈']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0:1] # 切片 只能取到0的元素，1的元素取不到"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7a8e7e19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小陈', '小梁', '小黄', '小刘', '小张']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d8e7fc41",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.append(\"小姜\") # append:追加，在尾部追加一个小姜"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ec843fef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小陈', '小梁', '小黄', '小刘', '小张', 7, '小姜']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "20785cd7",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "insert expected 2 arguments, got 1",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [9]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43ma\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minsert\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m小李\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mTypeError\u001b[0m: insert expected 2 arguments, got 1"
     ]
    }
   ],
   "source": [
    "a.insert(\"小李\") # 在元素0和1之间插入一个小李"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "65185b15",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小陈', '小梁', '小黄', '小刘', '小张', 7, '小姜']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "70e694bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.remove(7)  #删除一个指定的值 a.remove(值),如果有重复，只删除前面的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "42c73055",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小陈', '小梁', '小黄', '小刘', '小张', '小姜']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "65e2fe7d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'小姜'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.pop() #pop 将最后一个值弹出来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4a87d85c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小陈', '小梁', '小黄', '小刘', '小张']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a #再次查看a时，已经将最后一个值删除了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0e417daf",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.clear() # 这是将a的值全部清除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f69f3c09",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "8f25abe8",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.reverse() #取反"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "cf940d07",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ae9ae351",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Not an odd time.\n",
      "This minute seem a little odd.\n",
      "Not an odd time.\n",
      "This minute seem a little odd.\n",
      "This minute seem a little odd.\n"
     ]
    }
   ],
   "source": [
    "# p33练习\n",
    "from datetime import datetime\n",
    "import random\n",
    "import time\n",
    "\n",
    "odds = [1,3,4,5,6,7,8,0,15,17,19,13,24,28,39,45,68,88,99,100]\n",
    "\n",
    "for i in range(5):\n",
    "    right_this_minute = datetime.today().minute\n",
    "    if right_this_minute in odds:\n",
    "        print(\"This minute seem a little odd.\")\n",
    "    else:\n",
    "        print(\"Not an odd time.\")\n",
    "    wait_time = random. randint(1,100)\n",
    "    time.sleep(wait_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "874556bf",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (3541227315.py, line 4)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [22]\u001b[1;36m\u001b[0m\n\u001b[1;33m    for beer_num in range(99,0,-1)\u001b[0m\n\u001b[1;37m                                  ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# p44练习\n",
    "word = \"bottles\"\n",
    "\n",
    "for beer_num in range(99,0,-1)\n",
    "    print(beer_num,word,\"of beer on the wall.\")\n",
    "    print(beer_num,word,\"of beer.\")\n",
    "    print(\"take one down.\")\n",
    "    print(\"Pass it around.\")\n",
    "    if beer_num == 1:\n",
    "        print(\"No more bottles of beer on the wall.\")\n",
    "    else:\n",
    "        new_num = beer_num - 1\n",
    "        if new_num == 1:\n",
    "            word = \"bottle\"\n",
    "        print(new_num,word,\"of beer on the wall.\")\n",
    "        print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "888b257c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
